#include <iostream>
#include <string>
#include <map>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>

using namespace std;

// 创建一个英汉词典，方便翻译
map<string, string> dictionary = {
    // 基本问候语
    {"Hello", "你好"},
    {"Hi", "嗨"},
    {"Good morning", "早上好"},
    {"Good afternoon", "下午好"},
    {"Good evening", "晚上好"},
    {"Goodbye", "再见"},
    {"See you later", "待会儿见"},

    // 感谢与回应
    {"Thank", "谢谢"},
    {"Thanks", "谢谢"},
    {"Thank you very much", "非常感谢你"},
    {"You're welcome", "不客气"},
    {"No problem", "没问题"},

    // 人称代词
    {"You", "你"},
    {"Me", "我"},
    {"He", "他"},
    {"She", "她"},
    {"It", "它"},
    {"We", "我们"},
    {"They", "他们"},

    // 情感与态度
    {"Love", "爱"},
    {"Like", "喜欢"},
    {"Hate", "讨厌"},
    {"Happy", "快乐"},
    {"Sad", "悲伤"},

    // 常用名词
    {"World", "世界"},
    {"Home", "家"},
    {"School", "学校"},
    {"Work", "工作"},
    {"Friend", "朋友"},

    // 数字
    {"One", "一"},
    {"Two", "二"},
    {"Three", "三"},
    {"Four", "四"},
    {"Five", "五"},
    {"Ten", "十"},

    // 疑问词
    {"What", "什么"},
    {"Who", "谁"},
    {"Where", "哪里"},
    {"When", "什么时候"},
    {"Why", "为什么"},
    {"How", "怎么样"},

    // 其他常用短语
    {"Please", "请"},
    {"Sorry", "对不起"},
    {"Excuse me", "打扰一下"},
    {"Yes", "是"},
    {"No", "不是"}
};

// 处理客户端请求
void handleClient(int clientSocket) {
    char buffer[1024];
    int bytesRead;
    
    // 读取客户端发送的数据
    while ((bytesRead = read(clientSocket, buffer, sizeof(buffer))) > 0) {
        buffer[bytesRead] = '\0';  // 添加字符串终止符
        string word(buffer);

        // 查找翻译
        if (dictionary.find(word) != dictionary.end()) {
            string translated = dictionary[word];
            write(clientSocket, translated.c_str(), translated.length());
        } else {
            string notFound = "词汇未找到";
            write(clientSocket, notFound.c_str(), notFound.length());
        }
    }

    close(clientSocket);
}

int main() {
    int serverSocket;
    struct sockaddr_in serverAddr;
    fd_set readfds;
    int maxfd;

    // 创建TCP socket
    serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (serverSocket < 0) {
        perror("socket() failed");
        return 1;
    }

    // 设置服务器地址
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(8080);
    serverAddr.sin_addr.s_addr = INADDR_ANY;  // 绑定到所有网络接口（包括公网IP）

    // 绑定socket和地址
    if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
        perror("bind() failed");
        return 1;
    }

    // 设置为监听模式
    if (listen(serverSocket, 5) < 0) {
        perror("listen() failed");
        return 1;
    }

    // 初始化fd_set
    FD_ZERO(&readfds);
    FD_SET(serverSocket, &readfds);
    maxfd = serverSocket;

    cout << "服务器启动，等待客户端连接...\n";

    // 使用select模型监听多个连接
    while (true) {
        fd_set tempfds = readfds;
        if (select(maxfd + 1, &tempfds, NULL, NULL, NULL) < 0) {
            perror("select() failed");
            return 1;
        }

        // 检查是否有新的连接请求
        if (FD_ISSET(serverSocket, &tempfds)) {
            struct sockaddr_in clientAddr;
            socklen_t clientAddrLen = sizeof(clientAddr);
            int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
            if (clientSocket < 0) {
                perror("accept() failed");
                continue;
            }

            // 将新客户端加入到fd_set中
            FD_SET(clientSocket, &readfds);
            if (clientSocket > maxfd) {
                maxfd = clientSocket;
            }

            cout << "有新客户端连接，客户端IP: " << inet_ntoa(clientAddr.sin_addr) << endl;
        }

        // 遍历所有客户端连接
        for (int i = 0; i <= maxfd; ++i) {
            if (FD_ISSET(i, &tempfds) && i != serverSocket) {
                handleClient(i);  // 处理客户端请求
                FD_CLR(i, &readfds);  // 移除处理后的客户端连接
            }
        }
    }

    close(serverSocket);
    return 0;
}
